﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;
using T2V.SharePoint.Report.DocXGenerator;
using Pesico.Forms.Common;

namespace Pesico.Forms.DocxGenerator
{
    public class GeneratorFactory
    {
        private static DocumentGenerationInfo GetDocumentGenerationInfo(string docType, string docVersion, SPListItem item, object dataContext, string templateLibUrl, bool useDataBoundControls)
        {
            DocumentGenerationInfo generationInfo = new DocumentGenerationInfo();
            generationInfo.Metadata = new DocumentMetadata() { DocumentType = docType, DocumentVersion = docVersion };
            generationInfo.DataContext = dataContext;

            string fileTemplateUrl = templateLibUrl.Trim().TrimEnd('/') + "/" + "HopDongDaiLyTemplate.docx";

            if (T2V.SharePoint.Common.Utilities.Utility.IsAbsoluteUri(templateLibUrl))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(fileTemplateUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPFile file = web.GetFile(fileTemplateUrl);
                            generationInfo.TemplateData = file.OpenBinary();
                        }
                    }
                });
            }
            else
            {
                fileTemplateUrl = item.Web.ServerRelativeUrl.TrimEnd('/') + "/" + fileTemplateUrl.TrimStart('/');
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(item.Web.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb(item.Web.ID))
                        {
                            SPFile file = web.GetFile(fileTemplateUrl);
                            generationInfo.TemplateData = file.OpenBinary();
                        }
                    }
                });
            }
           
            generationInfo.IsDataBoundControls = useDataBoundControls;

            return generationInfo;
        }

        public static DocumentGenerator GetGenerator(SPListItem item, string templateLibUrl)
        {
             DocumentGenerationInfo generationInfo = GetDocumentGenerationInfo("DocxGenerator", "1.0", item, GetDataContext(item),
                                                    templateLibUrl, false);

            switch (item.ContentType.Parent.Id.ToString().ToLower())
            {
                case Constants.CONTRACT_CONTENT_TYPE_ID:
                    return new ContractGenerator(generationInfo);
            }

            return null;
        }

        private static object GetDataContext(SPListItem item)
        {
            switch (item.ContentType.Parent.Id.ToString().ToLower())
            {
                case Constants.CONTRACT_CONTENT_TYPE_ID:
                    return new ContractDataContext(item);
            }

            return null;
        }
    }
}
